package com.web.util;

import com.web.util.JsonResult;
import com.web.util.LoginException;
import com.web.util.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {

    @ExceptionHandler(Exception.class)
    public JsonResult<Void> serviceExceptionHandle(Exception e){
        JsonResult<Void> jsonResult;
        if(e instanceof ServiceException){
            //业务异常
            jsonResult = new JsonResult<>(400, e.getMessage(), null);
        } else if (e instanceof LoginException){
            //未登录
            jsonResult = new JsonResult<>(440, "请重新登录", null);
        } else {
            //其他
            e.printStackTrace();
            jsonResult = new JsonResult<>(500, "服务器异常", null);
        }
        return jsonResult;
    }
}
